<template>
    <div>
        </br>
        </br>

        </br>
        <div style="margin: 20px;"></div>
        <!-- <el-form label-width="450px" :model="formLabelAlign" > -->
            <el-form
        ref="formLabelAlign"
        :model="formLabelAlign"
        :rules="rules"
        label-width="350px"
        autocomplete="off"
        :hide-required-asterisk="true"
        size="medium"
      >
            <el-form-item label="题目" >
                <el-input v-model="formLabelAlign.subjectinfo" style="width:62.5%"></el-input>
            </el-form-item>
            <el-form-item label="A选项" prop="subjecta">
                <el-col :span="15">
                    <el-input v-model="formLabelAlign.subjecta" style="width:100%" />
                </el-col>
            </el-form-item>
            <el-form-item label="B选项" prop="subjectb">
                <el-col :span="15">
                    <el-input v-model="formLabelAlign.subjectb" style="width:100%" />
                </el-col>
            </el-form-item>
            <el-form-item label="C选项" prop="subjectc">
                <el-col :span="15">
                    <el-input v-model="formLabelAlign.subjectc" style="width:100%" />
                </el-col>
            </el-form-item>
            <el-form-item label="D选项" prop="subjectd">
                <el-col :span="15">
                    <el-input v-model="formLabelAlign.subjectd" style="width:100%" />
                </el-col>
            </el-form-item>
            <el-form-item label="">
                 <el-button :loading="loading" round type="primary" style="width:62.5%;margin-bottom:30px;" @click.native.prevent="submitForm">添加</el-button>
            </el-form-item>
       </el-form>
          

    </div>
</template>
<script>
import indexApi from '@/api/question/index.js'
import indexApi1 from '@/api/certification/index.js'

  export default {
    data() {
      return {
        formLabelAlign: {
          subjectinfo: '',
          subjecta: '',
          subjectb: '',
          subjectc: '',
          subjectd: '',
        },
        rules: {
          subjecta: [{
            required: true,
            message: '分数',
            trigger: 'blur'
            }, { pattern:/(^(\d|[1-9]\d)?$)|(^100$)/, message: '请输入0-100的分数.'}],
        subjectb: [{
            required: true,
            message: '分数',
            trigger: 'blur'
            }, { pattern:/(^(\d|[1-9]\d)?$)|(^100$)/, message: '请输入0-100的分数.'}],
        subjectc: [{
            required: true,
            message: '分数',
            trigger: 'blur'
            }, { pattern:/(^(\d|[1-9]\d)?$)|(^100$)/, message: '请输入0-100的分数.'}],
        subjectd: [{
            required: true,
            message: '分数',
            trigger: 'blur'
            }, { pattern:/(^(\d|[1-9]\d)?$)|(^100$)/, message: '请输入0-100的分数.'}],
        }
      };
    },
    created() { // 在渲染之前执行
        
         if (this.$route.params.id) {
            console.log(this.$route.params.id)
            this.fetchDataById(this.$route.params.id)
        }else{
            this.task()
        }
        
    },
    mounted() { // 在渲染之后执行   
       
       
    },
    methods:{

         task(){
            indexApi1.getemail().then(response => {
              const authority=response.data.authority
              if(authority===0){
                this.$alert('你不是管理员不能进行此操作', '权限认证', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$router.push(`/login`)
                        }
                    })
                }
              })
          } ,
        submitForm(){
            if(this.$route.params.id){
                this.update(this.$route.params.id)
            }else{
                this.add()
            }    
        },

        add(){
            this.$refs['formLabelAlign'].validate((valid) => {
               console.log(valid)
               if(valid){
                    this.$confirm('确定添加这道题到题库吗, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                        }).then(() => {
                        this.$message({
                            type: 'success',
                            message: '添加成功成功!'
                        });
                        indexApi.additive(this.formLabelAlign).then(response => {
                            this.$router.push({ path: '/list' })
                        })
                        
                        }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消添加'
                        });          
                        });
                    }
            });
            
        },

        update(id){
            this.$refs['formLabelAlign'].validate((valid) => {
               console.log(valid)
               if(valid){
                    this.$confirm('确定修改这道题吗, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                        }).then(() => {
                        this.$message({
                            type: 'success',
                            message: '修改成功!'
                        });
                        indexApi.updateid(this.formLabelAlign,id).then(response => {
                            this.$router.push({ path: '/list' })
                        })
                        
                        }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消修改'
                        });          
                        });
               }
            })
            
        },


        fetchDataById(id){
            console.log(id)
            indexApi.getbyid(id).then(response => {
                console.log(3)
                this.formLabelAlign=response.data
            })

        }
           
        
    }
}
</script>